package com.jemmyee.task.web.controller.front;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.core.web.Constants;
import com.jemmyee.core.web.utils.MathUtils;
import com.jemmyee.task.entity.UserNo;
import com.jemmyee.task.web.TaskConstants;
import com.jemmyee.task.web.controller.kernel.BaseTaskFrontController;


/**
 * @Description: TODO
 * @author:jemmyee@gmail.com
 * @date:2011-9-1
 * @version:v1.0
 */
@Controller("taskUserNoFrontController")
@RequestMapping(Constants.MV_URI_APP_TASK+Constants.MV_URI_MAIN_SSL+Constants.SYM_PATH_SEPARATOR+TaskConstants.ENTITY_USERNO) 
public class UserNoController extends BaseTaskFrontController{
	
	private static Log log= LogFactory.getLog(UserNoController.class);
	/*分页查出保存的条件*/
	private static final String ENTITY_SESSION_BEAN="admin_session_bean_"+MathUtils.getRandomDouble();
	private static final String ROOT_VIEW=Constants.MV_URI_MAIN_FRONT+Constants.MV_URI_APP_TASK+Constants.SYM_PATH_SEPARATOR+TaskConstants.ENTITY_USERNO;
	private static final String ROOT_URI=Constants.MV_URI_APP_TASK+Constants.MV_URI_MAIN_SSL+Constants.SYM_PATH_SEPARATOR+TaskConstants.ENTITY_USERNO;
	
	@RequestMapping("/toAdd/{type}")
	public ModelAndView toPersonal(@PathVariable Integer type,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.addObject("type", type);
		mv.setViewName(ROOT_VIEW+"/add"); 
		return mv;
	}
	@RequestMapping(Constants.MV_URI_COMM_ADD)
	@ResponseBody
	public Map<String, Object> add(@RequestBody UserNo bean,
			HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String, Object>();
		UserNo temp = null;
		try {
			if (userNoService.findByProperty("name", bean.getName()) != null
					&& userNoService.findByProperty("name", bean.getName())
							.size() > 0) {
				temp = userNoService.findByProperty("name", bean.getName()).get(
						0);
			}
			if (temp != null && bean.getName().equals(bean.getName().trim())) {
				model.put(Constants.AJAX_MSG_NAME, Constants.AJAX_MSG_RENAME);
			} else {
				bean.setUserId(getOnlineUser(request).getId());
				bean.setAddTime(new Date());
				bean.setIsDelete((short)0);
				bean.setIsDefault((short)0);
				Serializable id=userNoService.save(bean);
				initLog(getOnlineUser(request).getName(),"[绑定账号] dataid:"+id+",name:"+bean.getName()+"],type:"+bean.getType()+",platform:"+bean.getPlatform(), request);
				model.put(Constants.AJAX_MSG_NAME, Constants.AJAX_MSG_SUCC);
			}
		} catch (Exception e) {
			model.put(Constants.AJAX_MSG_NAME, Constants.AJAX_MSG_ERROR);
			log.error(e);
		}
		return model;
	}
	

	/**
	* @Description:店铺tab显示主框架
	* @author:jemmyee@gmail.com
	* @date:2011-9-1
	*/
	@RequestMapping("/toTabs/{type}/{platform}/{tabID}")
	public ModelAndView toTabs(@PathVariable Integer type,@PathVariable Integer platform,@PathVariable Integer tabID,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.addObject("type", type);
		mv.addObject("platform", platform);
		mv.addObject("tabID", tabID);
		mv.setViewName(ROOT_VIEW+"/tabs"); 
		return mv;
	}
	
	@RequestMapping(value ="/list/{from}/{type}/{platform}/{tabID}/{pageSize}/{startIndex}")
	public ModelAndView list(@PathVariable String from,
			@PathVariable Short type,@PathVariable Short platform,@PathVariable Integer tabID, @PathVariable Integer pageSize,
			@PathVariable Integer startIndex, @ModelAttribute UserNo bean,
			HttpServletRequest request) {
		DetachedCriteria dc = DetachedCriteria.forClass(UserNo.class);
		ModelAndView mv = new ModelAndView();
		UserNo temp = new UserNo();
		if (from.equals(Constants.PAGE_ENTRY_TYPE_FORM)) {
			temp = bean;
			request.getSession().setAttribute(ENTITY_SESSION_BEAN, bean);
		}
		if (from.equals(Constants.PAGE_ENTRY_TYPE_PAGE)) {
			if (request.getSession().getAttribute(ENTITY_SESSION_BEAN) != null) {
				temp = (UserNo) request.getSession().getAttribute(
						ENTITY_SESSION_BEAN);
			}
		}
		if (from.equals(Constants.PAGE_ENTRY_TYPE_MENU)) {

		}

		if (pageSize == 0) {
			pageSize = Constants.DEFAULT_PAGESIZE;
		}
		if(platform!=-1)//所有
		{
			dc.add(Restrictions.eq("platform",platform));
		}
		if(type!=-1)//所有
		{
			dc.add(Restrictions.eq("type",type));
		}
		dc.addOrder(Order.desc("addTime"));
		Page page = userNoService.findPageByCriteria(dc, pageSize, startIndex);
		if (pageSize != 0) {
			page.setPageSize(pageSize);
		}
		String pageURI=request.getContextPath()+Constants.SYM_PATH_SEPARATOR+ROOT_URI+"/"+type+"/"+platform+"/"+tabID;
		initList(mv,page,Constants.MV_VIEW_LIST,pageURI,ROOT_URI,temp,ROOT_VIEW,-1,pageSize,startIndex);
		return mv;
	}

	
}
